package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import cn.com.sinux.spring.json.JsonFilter;
import cn.com.sinux.spring.utils.DateUtils;
import cn.com.sinux.spring.utils.PageUtils;
import com.alibaba.fastjson.JSONObject;
import com.sinosoft.hydra.um.domain.permission.TAttendance;
import com.sinosoft.hydra.um.domain.permission.TMeeting;
import com.sinosoft.hydra.um.services.MeetingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sinosoft.hydra.um.helper.LoggerHelper;

import java.util.Date;
import java.util.List;

/**
 * Created by ZhangXin on 2018/8/03.
 */
@Controller
@RequestMapping(value="/api/meeting")
public class SysMeetingApi {
    @Autowired
    private MeetingService meetingService;

    @RequestMapping("reservation")
    public String toReservation() {
        return "hydra_easyui/meeting/reservation";
    }

    @RequestMapping("reservation/list")
    @RequiresPermissions(value = "meetingroom:reservation:all")
    @ResponseBody
    public Object listReservations(@RequestParam(value = "date",  required = false) Date date,
                                   @RequestParam(value = "rows", defaultValue = "10", required = false) Integer pageSize,
                                   @RequestParam(value = "page", defaultValue = "1", required = false) Integer pageNumber)
    {
        Pageable pageable = PageUtils.buildPageRequest(pageNumber, pageSize, "beginTimeIndex", "asc");

        Date searchDate = date == null? new Date() : date;
        Page<TMeeting> page = meetingService.findReservationsForDate(searchDate, pageable);
        return page;
    }

    @RequestMapping("reservation/add")
    @RequiresPermissions(value = "meetingroom:reservation:all")
    @ResponseBody
    public Object add(TMeeting meeting){
        // check whether there is any conflict
        if(meetingService.isMeetingConflictWithExisting(meeting)) {
            JSONObject json = new JSONObject();
            json.put("isConflict", "true");
            return json;
        }

        TMeeting meetingInstance = meetingService.addMeeting(meeting);
        return JsonFilter.doPropertyFilter(meetingInstance, new PropertyCollection()
                .addProperties("id"));
    }

    @RequestMapping("reservation/delete")
    @RequiresPermissions(value = "meetingroom:reservation:all")
    @ResponseBody
    public Object delete(String id){
        TMeeting meeting = meetingService.deleteMeeting(id);
        return JsonFilter.doPropertyFilter(meeting, new PropertyCollection()
                .addProperties("id"));
    }
}
